package com.hengpeng.itfin.domain.partner;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.configuration.ConfigProvider;
import com.hengpeng.common.configuration.ConfigProviderFactory;
import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfin.service.ServiceFactory;
import com.hengpeng.itfinbase.persist.Partner;

/**
 * <pre>
 * 抽象预付款网关处理器
 * </pre>
 * @author Richard
 * @version 1.0, 2013-7-9
 */
public abstract class AbstractPartnerProcessor implements PartnerProcessor
{
	public Log logger = LogFactory.getLog(this.getClass());

	public final static ConfigProvider<String> cp = ConfigProviderFactory
			.getInstance(Constant.HP_COMMON_PROPERTIES_FILEPATH);

	protected ServiceFactory serviceFactory;

	protected Partner partner;

	public static final String CONFIG_PAGE_RETURNURL = "page_return_url";

	public static final String CONFIG_SERVICE_NOTIFYURL = "service_notify_url";

	public String page_return_url = cp.getConfigByPrimaryKey("page.return.url");

	public String service_notify_url = cp.getConfigByPrimaryKey("service.notify.url");

	public Map<String, String> extConfigs;

	public AbstractPartnerProcessor(ServiceFactory serviceFactory, Partner partner)
	{
		this.partner = partner;
		this.serviceFactory = serviceFactory;
		extConfigs = partner.getExtConfigs();
		if (StringHelper.isEmpty(page_return_url))
		{
			page_return_url = extConfigs.get(CONFIG_PAGE_RETURNURL);
		}
		if (StringHelper.isEmpty(service_notify_url))
		{
			service_notify_url = extConfigs.get(CONFIG_SERVICE_NOTIFYURL);
		}
	}

	public class StringLinker
	{
		private StringBuffer buffer = new StringBuffer();

		public StringLinker append(String str)
		{
			if (str != null)
				buffer.append(str.trim());
			return this;
		}

		public String toString()
		{
			return this.buffer.toString();
		}
	}

	public class ParamHash
	{
		private HashMap<String, String> params = new HashMap<String, String>();

		public ParamHash append(String key, String value)
		{
			if (key != null && value != null)
				params.put(key, value);

			return this;
		}

		public HashMap<String, String> getParams()
		{
			return this.params;
		}
	}

	public static Map<String, String> getFieldVlaue(Object obj) throws Exception
	{
		Map<String, String> mapValue = new HashMap<String, String>();
		Class<?> cls = obj.getClass();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields)
		{
			String name = field.getName();
			String strGet = "get" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
			Method methodGet = cls.getDeclaredMethod(strGet);
			Object object = methodGet.invoke(obj);
			String value = object != null ? object.toString() : "";
			mapValue.put(name, value);
		}
		return mapValue;
	}

	/**
	 * 生成自动提交页面的内容字符串
	 * @param contentType 内容类型
	 * @param title 页面名称
	 * @param url 提交的页面URL
	 * @param parameters 提交的字段值映射表
	 * @param method 提交的方式,POST或者GET
	 * @return html字符串
	 */
	public static String autoSubmitHtmlFormJSON(String contentType, String title, String url,
			Map<String, String> parameters, String method)
	{
		if (method == null)
		{
			method = "POST";
		}
		StringBuffer sb = new StringBuffer();
		if (method.equalsIgnoreCase("POST"))
		{
			sb.append("<html>\r\n");
			sb.append("<head>\r\n");
			sb.append("<title>" + title + "</title>\r\n");
			sb.append("<meta http-equiv=\"Content-Type\" content=\"" + contentType + "\">\r\n");
			sb.append("<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n");
			sb.append("<meta http-equiv=\"Pragma\" content=\"no-cache\">\r\n");
			sb.append("<meta http-equiv=\"Expires\" content=\"0\">\r\n");
			sb.append("</head>\r\n");
			sb.append("<body>\r\n");
			sb.append("正在提交订单,请稍候......\r\n");
			sb.append("\r\n");
			sb.append("<form name=\"sendOrder\" method=\"" + method + "\" action=\"" + url + "\">\r\n");
			sb.append("<table>\r\n");
			Set<Map.Entry<String, String>> set = parameters.entrySet();
			Iterator<Map.Entry<String, String>> ite = set.iterator();
			while (ite.hasNext())
			{
				Map.Entry<String, String> e = ite.next();
				sb.append("<tr><td>" + "<textarea rows=\"1\" cols=\"1\" style=\"display: none;\" name=\"" + e.getKey()
						+ "\">" + e.getValue() + "</textarea>" + "</td></tr>\r\n");
			}
			sb.append("</table>\r\n");
			sb.append("</form>\r\n");
			sb.append("\r\n");
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("document.sendOrder.submit();\r\n");
			sb.append("</script>\r\n");
			sb.append("</body>\r\n");
			sb.append("</html>\r\n");
		}
		else
		{
			StringBuffer urlBuf = new StringBuffer(url);
			Set<Map.Entry<String, String>> set = parameters.entrySet();
			Iterator<Map.Entry<String, String>> ite = set.iterator();
			urlBuf.append("?");
			while (ite.hasNext())
			{
				Map.Entry<String, String> e = ite.next();
				urlBuf.append(e.getKey() + "=" + e.getValue() + "&");
			}

			sb.append("<html>\r\n");
			sb.append("<head>\r\n");
			sb.append("<title>" + title + "</title>\r\n");
			sb.append("<meta http-equiv=\"Content-Type\" content=\"" + contentType + "\">\r\n");
			sb.append("<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n");
			sb.append("<meta http-equiv=\"Pragma\" content=\"no-cache\">\r\n");
			sb.append("<meta http-equiv=\"Expires\" content=\"0\">\r\n");
			sb.append("</head>\r\n");
			sb.append("<body>\r\n");
			sb.append("正在提交订单,请稍候......\r\n");
			sb.append("\r\n");
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("window.location = \"" + urlBuf.toString() + "\"\r\n");
			sb.append("</script>\r\n");
			sb.append("</body>\r\n");
			sb.append("</html>\r\n");
		}
		return sb.toString();
	}

}
